package com.qzsoft.zhjd.server.modules.cement.handler;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.qzsoft.zhjd.server.modules.cement.entity.CollectCementAlarmEntity;
import com.qzsoft.zhjd.server.modules.cement.entity.CollectCementDataRatioEntity;

/**
 * @ClassName CementAlarmHandler
 * @Description 预警处理
 * @author linhuawei
 * @since 2022-02-28
 */
public class CementAlarmHandler {

    /**
     * @Title coverTo
     * @Description 处理预警信息
     * @param alarmEntity 预警对象
     * @param dataRatio   占比数据
     */
    public static CollectCementAlarmEntity coverTo(CollectCementDataRatioEntity dataRatio) {
        CollectCementAlarmEntity alarmEntity = new CollectCementAlarmEntity();
        alarmEntity.setGravel1(0);
        alarmEntity.setGravel2(0);
        alarmEntity.setGravel3(0);
        alarmEntity.setGravel4(0);
        alarmEntity.setGravel5(0);
        alarmEntity.setGravel6(0);
        alarmEntity.setPowder1(0);
        alarmEntity.setPowder2(0);
        alarmEntity.setWater(0);
        alarmEntity.setAlarm(0);

        List<String> alarmFields = new ArrayList<String>();
        int alarmTemp = countAlarm(dataRatio.getGravel1RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("集料1");
            alarmEntity.setGravel1(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getGravel2RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("集料2");
            alarmEntity.setGravel2(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getGravel3RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("集料3");
            alarmEntity.setGravel3(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getGravel4RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("集料4");
            alarmEntity.setGravel4(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getGravel5RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("集料5");
            alarmEntity.setGravel5(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getGravel6RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("集料6");
            alarmEntity.setGravel6(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getPowder1RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("粉料1");
            alarmEntity.setPowder1(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getPowder2RatioError());
        if (0 < alarmTemp) {
            alarmFields.add("粉料2");
            alarmEntity.setPowder2(alarmTemp);
        }

        alarmTemp = countAlarm(dataRatio.getWaterRatioError());
        if (0 < alarmTemp) {
            alarmFields.add("水");
            alarmEntity.setWater(alarmTemp);
        }

        if (0 < alarmFields.size()) {
            StringBuffer alarmDetails = new StringBuffer();
            for (int i = 0, len = alarmFields.size(); i < len; i++) {
                if (0 < i) {
                    alarmDetails.append("、");
                }
                alarmDetails.append(alarmFields.get(i));
            }
            alarmDetails.append("占比超标");

            alarmEntity.setAlarm(1);// 报警
            alarmEntity.setDetails(alarmDetails.toString());
            alarmEntity.setState(0);// 未处理
        }

        return alarmEntity;
    }

    /**
     * @Title countAlarm
     * @Description 判断是否报警
     * @param error 误差值
     * @return
     */
    private static int countAlarm(BigDecimal error) {
        if (null == error) {
            return 0;
        }

        // 如果误差绝对值大于10，认为是超标
        if (1 == error.abs().compareTo(BigDecimal.TEN)) {
            return 1;
        }
        return 0;
    }

}
